import { getAjax, postAjax, putAjax, deleteAjax } from '@/utils/request'
import { Mutation, Action } from 'vuex'
import { DictData } from '../system/dictData'
export interface Job {
  jobId: number | null;
  jobName: string;
  jobGroup: string;
  invokeTarget?: string;
  cronExpression?: string;
  misfirePolicy?: string;
  concurrent?: boolean;
  status?: boolean;
  businessType?: string;
}

export interface StateType {
  data: Job;
  jobGroupOptions: Array<DictData>;
}

export interface ModuleType {
  namespaced: boolean;
  state: StateType;
  mutations: {
    SET_DATA: Mutation<StateType>;
    SET_JOB_GROUP_OPTION: Mutation<StateType>;
  };
  actions: {
    getList: Action<StateType, StateType>;
    getJobGroupOptions: Action<StateType, StateType>;
    getInfo: Action<StateType, StateType>;
    add: Action<StateType, StateType>;
    update: Action<StateType, StateType>;
    delete: Action<StateType, StateType>;
    changeStatus: Action<StateType, StateType>;
    run: Action<StateType, StateType>;
  };
}

const storeModel: ModuleType = {
  namespaced: true,
  state: {
    data: {
      jobId: -1,
      jobName: '',
      jobGroup: ''
    },
    jobGroupOptions: []
  },
  mutations: {
    SET_DATA: (state, data) => {
      state.data = data
    },
    SET_JOB_GROUP_OPTION: (state, data) => {
      state.jobGroupOptions = data
    }
  },
  actions: {
    getList(_, data) {
      return getAjax('/monitor/job/list', data)
    },
    getJobGroupOptions({ commit, dispatch }) {
      dispatch('dictData/getDicts', 'sys_job_group', { root: true }).then(res => {
        commit('SET_JOB_GROUP_OPTION', res.data)
      })
    },
    getInfo(_, id) {
      return getAjax(`/monitor/job/${id}`, null)
    },
    add(_, data) {
      return postAjax('/monitor/job', data)
    },
    update(_, data) {
      return putAjax('/monitor/job', data)
    },
    delete(_, id) {
      return deleteAjax(`/monitor/job/${id}`, null)
    },
    changeStatus(_, data) {
      return putAjax('/monitor/job/changeStatus', data)
    },
    run(_, data) {
      return putAjax('/monitor/job/run', data)
    }
  }
}

export default storeModel
